//
//  LTFMyCollectViewController.m
//  NeteaseNews
//
//  Created by liutaifeng on 16/5/1.
//  Copyright © 2016年 liutaifeng. All rights reserved.
//

#import "LTFMyCollectViewController.h"
#import "LTFTopicBaseHandel.h"
#import "LTFTopicModel.h"
#import "LTFTopicTopView.h"
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"
#import "LTFCollectTableViewCell.h"
#import "LTFTopicContentViewController.h"
@interface LTFMyCollectViewController ()
<
 UITableViewDataSource,
 UITableViewDelegate
>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) NSMutableArray *urlArray;
@end

@implementation LTFMyCollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createView];
}

- (void)createView {
    self.urlArray = [NSMutableArray array];
    self.array = [NSMutableArray array];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[LTFCollectTableViewCell class] forCellReuseIdentifier:NSStringFromClass([LTFCollectTableViewCell class])];
    _tableView.rowHeight = 120;
    
    
    
    
}
- (void)viewWillAppear:(BOOL)animated {
    NSMutableArray *array = [[LTFTopicBaseHandel shareDataBaseTool] select];
    for (NSString *string in array) {
        [self.urlArray addObject:string];
        [self createData:string];
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LTFCollectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LTFCollectTableViewCell class])];
    
    [cell.picImageView setImageWithURL:[NSURL URLWithString:self.array[indexPath.row]] placeholderImage:[UIImage imageNamed:@"zhanwei.png"]];
        return cell;
}
- (void)createData:(NSString *)string {
    NSString *str = [NSString stringWithFormat:@"http://c.3g.163.com/newstopic/qa/%@.html", string];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:str parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dictionary = responseObject;
        NSDictionary *data = dictionary[@"data"];
        NSDictionary *expert = data[@"expert"];
        [self.array addObject:expert[@"picurl"]];
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        LTFTopicContentViewController *topicContentVC = [[LTFTopicContentViewController alloc] init];
        topicContentVC.string = self.urlArray[indexPath.row];
        [self.navigationController pushViewController:topicContentVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath  {
    UITableViewRowAction *firstAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        //要实现删除, 第一件事就是删掉对应数组里的数据
        [self.array removeObjectAtIndex:indexPath.row];
        [[LTFTopicBaseHandel shareDataBaseTool] deleteCollect:self.urlArray[indexPath.row]];
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
        
    }];
    firstAction.backgroundColor = [UIColor redColor];
    return @[firstAction];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
